<?php


define('DRUPAL_ROOT', getcwd());
$base_url = 'http://'.$_SERVER['HTTP_HOST']; // THIS IS IMPORTANT
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // Could be DRUPAL_BOOTSTRAP_SESSION if that's all you need.
global $user;
global $base_url;
global $language;	

if ($_REQUEST['process'] == 'get-desearch') {       //retrives addresstext of selected address in pickup and drop off address
    $dep = $_REQUEST['addr'];
	$lang = $_REQUEST['lang'];
	if($dep){
		$dep = strtolower(str_replace('  ', ' ', $dep));
		$data = explode(" ", $dep);
		foreach($data as $key => $value) {
			$conds[] = "AND LOWER(replace(a.title, ' ', ' ')) LIKE '%%" . trim($value) . "%'";
							/*OR LOWER(replace(a.title, ' ', ' ')) LIKE '" . trim($value) . "%'
							OR LOWER(replace(a.title, ' ', ' ')) LIKE '%%" . trim($value) . "'";*/
		}
		//print_r($language);
		$cond = implode(" OR ", $conds);
	 	my_db_change(1);
		$query = db_select('node','a');
		$query->join('field_data_field_scheme_concern_department','b','a.nid = b.entity_id AND a.vid = b.revision_id AND a.status = 1'.$cond .'');
		$query->fields('a',array('nid','title','vid'));
		$query->fields('b',array('entity_id'));
		$query->condition('a.language',$lang);
		$query->orderBy('title','ASC');
		$query->distinct('title','nid');
		$query_result=$query->extend('PagerDefault')->limit(19)->execute();	//print_r($query_result);
		my_db_change(0);
		
		foreach($query_result as $row){		
			$output .= '<div class="scheme_list"><p><a href="'.$base_url.'/tnportal_latest/scheme/data_view/'.$row->nid.'">'.substr($row->title,0,255).'</a></p></div>';			
			$title=$row->title;
			$nid=$row->nid;
		}
		//drupal_add_html_head_link(array('rel' => 'next', 'href' => url($_GET['q'], array('query' => $query_result))));
		//$output .= theme('pager');		
		print $output;
	}
}
?>